﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Models;
namespace AGVControlSystemPro
{
    public partial class FrmAGVEdit : Form
    {
        private AGVInfoManager objAGVInfoManager = new AGVInfoManager();
        private List<AGVInfo> listAGV = new List<AGVInfo>();//创建一个存储所有AGV信息的集合
        public  ShowInfo showAGVInfo; // //根据委托创建委托对象
        private bool updateFlag = false; //是否更新标志位
        public FrmAGVEdit()
        {
            InitializeComponent();
            //AGVl类型下拉框数据加载
            this.cboTypeName.DataSource = new BLL.AGVTypeManager().GetAllAGVType();
            this.cboTypeName.DisplayMember = "TypeName";
            this.cboTypeName.ValueMember = "TypeID";
            this.cboTypeName.SelectedIndex = -1;
            //轨道类型下拉数据加载
            this.cboTrackName.DataSource = new BLL.TrackInfoManager().GetAllTrack();
            this.cboTrackName.DisplayMember = "TrackName";
            this.cboTrackName.ValueMember = "TrackId";
            this.cboTrackName.SelectedIndex = -1;
            //AGV信息加载
            this.dgvAGVList.AutoGenerateColumns = false;
            ShowAGVInfo();
        }

        private void FrmAGVEdit_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmAGVEdit = null;//当窗体关闭时，将窗体对象清理掉
            if (updateFlag) showAGVInfo();
                
        }

        private void dgvAGVList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvAGVList, e);
        }
        /// <summary>
        /// 数据验证
        /// </summary>
        private void VerifyInputData()
        {
            #region 数据验证
            if (this.txtAGVId.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("番号を入力してください", "情報のヒント");
                else MessageBox.Show("请输入AGV编号", "信息提示");
                this.txtAGVId.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveInteger(this.txtAGVId.Text.Trim()))
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("番号入力フォーマットの誤りは、正の整数のために必要です", "情報のヒント");
                else MessageBox.Show("AGV编号输入格式错误，必须为正整数", "信息提示");
                this.txtAGVId.Focus();
                return;
            }
            if (this.txtAGVName.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("名前を入力してください", "情報のヒント");
                else MessageBox.Show("请输入AGV名称", "信息提示");
                this.txtAGVName.Focus();
                return;
            }
            if (this.txtIP.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレスを入力してください", "情報のヒント");
                else MessageBox.Show("请输入IP地址", "信息提示");
                this.txtIP.Focus();
                return;
            }
            if (!Common.DataValidate.IsIPAddress(this.txtIP.Text.Trim()))
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレスの入力フォーマットが間違っていますので、再入力してください", "情報のヒント");
                else MessageBox.Show("IP地址输入格式不对，请重新输入", "信息提示");
                this.txtIP.SelectAll();
                this.txtIP.Focus();
                return;
            }
            if (this.txtPort.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("ポート番号を入力してください", "情報のヒント");
                else MessageBox.Show("请输入端口号", "信息提示");
                this.txtPort.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtPort.Text.Trim()))
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("ポート番号は正整数または0でなければならない", "情報のヒント");
                else MessageBox.Show("端口号必须为正整数或0", "信息提示");
                this.txtPort.SelectAll();
                this.txtPort.Focus();
                return;
            }
            if (this.cboAGVStatus.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("禁用状態を選択してください", "情報のヒント");
                else MessageBox.Show("请选择AGV禁用状态", "信息提示");
                this.cboAGVStatus.Focus();
                return;
            }
            if (this.cboTypeName.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("AGVタイプを選択してください", "情報のヒント");
                else MessageBox.Show("请选择AGV类型", "信息提示");
                this.cboTypeName.Focus();
                return;
            }
            if (this.cboTrackName.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("AGVの所属軌道を選択してください", "情報のヒント");
                else MessageBox.Show("请选择AGV所属轨道", "信息提示");
                this.cboTrackName.Focus();
                return;
            }
            #endregion
        
        }
        //数据添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //数据验证
            VerifyInputData();
            //数据封装
            AGVInfo objAGVInfo = new AGVInfo()
            {
                AGVId = Convert.ToInt32(this.txtAGVId.Text.Trim()),
                AGVName = this.txtAGVName.Text.Trim(),
                IP = this.txtIP.Text.Trim(),
                Port = Convert.ToInt32(this.txtPort.Text.Trim()),
                AGVStatus = this.cboAGVStatus.SelectedIndex,
                TrackId =Convert.ToInt32( this.cboTrackName.SelectedValue),
                TypeID = Convert.ToInt32(this.cboTypeName.SelectedValue)
            };
            try
            {
                //后台访问
                int result = objAGVInfoManager.AddAGVInfo(objAGVInfo);
                if (result == 1)
                {
                    DialogResult dResult ;
                    if (CommonMethods.LanguageId == 3) dResult = MessageBox.Show("成功を加える，追加するかどうか？", "追加", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    else dResult =MessageBox.Show("AGV信息添加成功，是否继续添加？", "添加", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dResult == DialogResult.OK)
                    {
                        //数据清空
                        this.txtAGVId.Clear();
                        this.txtAGVName.Clear();
                        this.txtIP.Clear();
                        this.cboAGVStatus.SelectedIndex = -1;
                        this.cboTrackName.SelectedIndex = -1;
                        this.cboTypeName.SelectedIndex = -1;
                        ShowAGVInfo();
                        updateFlag = true; //更新置位
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {
                    if (CommonMethods.LanguageId == 3) MessageBox.Show("失敗を加える", "追加");
                    else MessageBox.Show("AGV信息添加失败", "添加");
          
                }
                    
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }            
        }
        //修改
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvAGVList.CurrentRow == null && this.dgvAGVList.Rows.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("選択できないようにする", "ヒントを修正する");
                else MessageBox.Show("请选择需要修改的AGV", "修改提示");
                return;
            }
            //数据验证
            VerifyInputData();
            //[1]封装数据
            AGVInfo objAGVInfo = new AGVInfo()
            {
                AGVId = Convert.ToInt32(this.txtAGVId.Text.Trim()),
                AGVName = this.txtAGVName.Text.Trim(),
                IP = this.txtIP.Text.Trim(),
                Port = Convert.ToInt32(this.txtPort.Text.Trim()),
                AGVStatus = this.cboAGVStatus.SelectedIndex,
                TrackId = Convert.ToInt32(this.cboTrackName.SelectedValue),
                TypeID = Convert.ToInt32(this.cboTypeName.SelectedValue)
            };
            try
            {
                int result = objAGVInfoManager.ModifyAGVInfo(objAGVInfo);
                if (result == 1)
                {
                    if (CommonMethods.LanguageId == 3) MessageBox.Show("番号：" + objAGVInfo.AGVId + "成功を修正する", "ヒントを修正する");
                    else MessageBox.Show("编号：" + objAGVInfo.AGVId + "修改成功", "修改提示");
                    ShowAGVInfo();
                    updateFlag = true; //更新置位
                }
                else
                {
                    if (CommonMethods.LanguageId == 3) MessageBox.Show("失敗を修正する", "変更する");
                    else MessageBox.Show("修改失败", "修改提示");
                } 
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
            
        }
        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvAGVList.CurrentRow == null && this.dgvAGVList.Rows.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("削除したデータを選択してください。", "ヒントを削除");
                else MessageBox.Show("请选择删除的数据", "删除提示");
                return;
            }
            string agvId = this.dgvAGVList.CurrentRow.Cells["AGVId"].Value.ToString();//获取AGVId
            try
            {
                DialogResult dResult;
                if (CommonMethods.LanguageId == 3) dResult = MessageBox.Show("削除するかどうか,番号：" + agvId + "データ", "ヒントを削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                else
                    dResult = MessageBox.Show("是否删除编号：" + agvId + "AGV信息", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dResult == DialogResult.OK)
                {
                    int result = objAGVInfoManager.DeleteAGVInfo(agvId);
                    if (result == 1)
                    {
                        ShowAGVInfo();
                        updateFlag = true; //更新置位
                    }
                    else
                    {
                        if (CommonMethods.LanguageId == 3) MessageBox.Show("失敗を削除する", "削除");
                        else
                            MessageBox.Show("删除失败", "删除提示");
                    } 
                }
               
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 所有AGV信息显示
        /// </summary>
        private void ShowAGVInfo()
        {
            this.dgvAGVList.DataSource = null;
            string agvStatus = "";
            listAGV = objAGVInfoManager.GetAGVInfo(agvStatus);

            this.dgvAGVList.DataSource = listAGV;
        }

        private void dgvAGVList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtAGVId.Text = this.dgvAGVList.CurrentRow.Cells["AGVId"].Value.ToString();
            this.txtAGVName.Text = this.dgvAGVList.CurrentRow.Cells["AGVName"].Value.ToString();
            this.txtIP.Text = this.dgvAGVList.CurrentRow.Cells["IP"].Value.ToString();
            this.txtPort.Text = this.dgvAGVList.CurrentRow.Cells["Port"].Value.ToString();
            this.cboAGVStatus.SelectedIndex = Convert.ToInt32(this.dgvAGVList.CurrentRow.Cells["AGVStatus"].Value);
            this.cboTypeName.Text = this.dgvAGVList.CurrentRow.Cells["TypeName"].Value.ToString();
            this.cboTrackName.Text = this.dgvAGVList.CurrentRow.Cells["TrackName"].Value.ToString();
        }

       
    }
}
